<?php

namespace App\Admin\Controllers;

use App\Models\Book;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class BookController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '书籍列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Book);

        $grid->column('id', __('Id'));
        $grid->column('cover', __('封面'))->image(200, 100);
        $grid->column('name', __('名称'))->expand(function ($model){
            $books = $model->chapters()->orderBy('sort', 'asc')->get()->map(function ($comment){
                return $comment->only(['name', 'sort']);
            });

            return new Table(['章节名称', '排序'], $books->toArray());
        });

        $grid->column('share_card', __('分享卡片'))->image(200, 100);
        $grid->column('share_title', __('分享标题'));

        $grid->column('author', __('作者'));
        $grid->column('word_ct', __('单词数'))->sortable();
        $grid->column('study_ct', __('学习人数'))->sortable();
        $grid->column('complete_ct', __('完成人数'))->sortable();
        $grid->column('origin_price', __('原价(分)'))->sortable();
        $grid->column('price', __('现价(分)'))->sortable();
        $grid->column('rate', __('折扣'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));
        $grid->column('active_ct', __('访问人数'));
        $grid->column('new_ct', __('新用户数'));
        $grid->column('auth_ct', __('授权人数'));
        $grid->column('pay_ct', __('付费人数'));
        $grid->column('pay_chapter_ct', __('单章节购买人数'));
        $grid->column('pay_all_chapter_ct', __('剩余章节购买人数'));
        $grid->column('member_ct', __('大会员购买人数'));
        $grid->column('income', __('合计收入'));

        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->like('name', '名称');
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Book::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('cover', __('Cover'));
        $show->field('name', __('Name'));
        $show->field('author', __('Author'));
        $show->field('word_ct', __('Word ct'));
        $show->field('study_ct', __('Study ct'));
        $show->field('complete_ct', __('Complete ct'));
        $show->field('price', __('Price'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Book);

        $form->image('cover', __('封面'))->move('book/cover')->uniqueName();
        $form->text('name', __('名称'))->rules('required');
        $form->text('author', __('作者'))->rules('required');

        $form->image('share_card', __('分享卡片'))->move('book/share/card')->uniqueName();
        $form->text('share_title', __('分享标题'))->rules('required');
        //$form->number('word_ct', __('单词数'))->default(10)->rules('required');
        //$form->number('study_ct', __('Study ct'));
        //$form->number('complete_ct', __('Complete ct'));
        $form->number('origin_price', __('原价/分'))->default(0)->rules('required');
        $form->number('price', __('现价/分'))->default(0)->rules('required');
        $form->number('rate', __('折扣（90表示9折，100表示不打折）'))->default(100)->rules('required');

        return $form;
    }
}
